// CDialogLogin.cpp : implementation file
//

#include "stdafx.h"
#include "ECGWorkStation.h"
#include "DialogLogin.h"
#include "..\public\BiocareDB\BiocareDB.h"
#include "..\public\pubfun\inireadwrite\inireadwrite.h"
#include "Globals.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialogLogin dialog


CDialogLogin::CDialogLogin(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogLogin::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogLogin)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDialogLogin::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogLogin)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogLogin, CDialog)
	//{{AFX_MSG_MAP(CDialogLogin)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogLogin message handlers

BOOL CDialogLogin::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CString strLastUserName = "";
	CIniReadWrite::GetMe()->LoadString(DEF_GENERAL_SETTING, DEF_LAST_USERNAME, &strLastUserName);
	if(strLastUserName.GetLength() <= 0) strLastUserName = "admin";

	((CEdit*)GetDlgItem(IDC_EDIT_USERNAME))->SetWindowText(strLastUserName);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDialogLogin::OnOK() 
{
	BeginWaitCursor();
	if(!CheckUserAndPassWord()){
		CString strErrorMsg = "";
		strErrorMsg.LoadString(IDS_STRING118);
		AfxMessageBox(strErrorMsg);

		return;
	}
	EndWaitCursor();

	CString strLastUserName = "";
	((CEdit*)GetDlgItem(IDC_EDIT_USERNAME))->GetWindowText(strLastUserName);
	if(strLastUserName.GetLength() <= 0) strLastUserName = "admin";
	CIniReadWrite::GetMe()->SaveString(DEF_GENERAL_SETTING, DEF_LAST_USERNAME, strLastUserName);

	CDialog::OnOK();
}

BOOL CDialogLogin::CheckUserAndPassWord(void)
{
	CString strUserName = "";
	((CEdit*)GetDlgItem(IDC_EDIT_USERNAME))->GetWindowText(strUserName);

	CString strPassWord = "";
	((CEdit*)GetDlgItem(IDC_EDIT_PASSWORD))->GetWindowText(strPassWord);

	CECGWorkStationApp* pApp = (CECGWorkStationApp*)AfxGetApp();
	if (!strUserName.CompareNoCase("admin") && !strPassWord.CompareNoCase("master")) {
		pApp->m_nUserType = USER_TYPE_ADMIN;
		pApp->m_strUserName = "admin";
		pApp->m_strUserID = "32";
		return TRUE;
	}

	CString strConnection = "";
	CIniReadWrite::GetMe()->LoadString(DEF_GENERAL_SETTING, DEF_DB_CONNECTION, &strConnection);

	char szErrorMsg[1024];
	memset(szErrorMsg, 0, 1024);
	BCDBDataSets* pDataSets = NULL;
	BCDBCreateDataSets(&pDataSets, szErrorMsg);
	BCDBDataSet* pDataSet = NULL;
	BCDBCreateDataSet(&pDataSet, szErrorMsg);

	BCDBDataElem* pData = NULL;
	BCDBCreateDataElem(&pData, T_USER_NAME, strUserName.GetLength(), DT_STRING, (void*)(LPCTSTR)strUserName, szErrorMsg);
	pDataSet->push_back(pData);
	pData = NULL;

	BCDBCreateDataElem(&pData, T_USER_PASSWORD, strPassWord.GetLength(), DT_STRING, (void*)(LPCTSTR)strPassWord, szErrorMsg);
	pDataSet->push_back(pData);
	pData = NULL;

	BOOL bRet = FALSE;
	BCDBConnection DBConn;
	if(!BCDBConnectDB(&DBConn, (LPCTSTR)pApp->m_strConnection, (LPCTSTR)theApp.m_strConnUserName, (LPCTSTR)theApp.m_strConnPassWord, szErrorMsg)) return FALSE;
	if(!BCDBQueryDBData(&DBConn, T_USER, pDataSet, &pDataSets, szErrorMsg)){
		BCDBDisconnectionDB(&DBConn, szErrorMsg);
		BCDBClearDataSet(pDataSet, szErrorMsg);
		BCDBClearDataSets(pDataSets, szErrorMsg);

		return FALSE;
	}
	BCDBDisconnectionDB(&DBConn, szErrorMsg);

	BCDBDataSets::iterator itDataSets = pDataSets->begin();
	if (itDataSets == pDataSets->end()) {
		BCDBClearDataSet(pDataSet, szErrorMsg);
		BCDBClearDataSets(pDataSets, szErrorMsg);

		return FALSE;
	}

	BCDBDataSet* pDataSetResult = *itDataSets;
	BCDBDataSet::iterator itDataSet;
	for (itDataSet = pDataSetResult->begin(); itDataSet != pDataSetResult->end(); ++itDataSet)
	{
		pData = *itDataSet;
		if (!strncmp(pData->szName, T_USER_TYPE, strlen(T_USER_TYPE))) {
			pApp->m_nUserType = atoi((const char*)pData->pValue);
		}
		if (!strncmp(pData->szName, T_USER_ID, strlen(T_USER_ID))) {
			pApp->m_strUserID.Format("%s", (char*)(pData->pValue));
		}
		pData = NULL;
	}
	pApp->m_strUserName = strUserName;

	BCDBClearDataSet(pDataSet, szErrorMsg);
	BCDBClearDataSets(pDataSets, szErrorMsg);

	return TRUE;
}



